package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.CuccServiceOfferConsts;
import com.iwhalecloud.bss.kite.dataservice.cache.OfferCache;
import com.iwhalecloud.bss.kite.dataservice.entity.spec.Offer;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <Description> 退货类型 <br>
 *
 * @version 1.0<br>
 * @see com.iwhalecloud.bss.kite.web.service.accept.attr.handler <br>
 * @since R9.0<br>
 */
@Service
public class SalesReturnTypeAttrHandler extends AbstractAttrHandler {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    private IOperateSceneInstService operateSceneInstService;

    @Override
    public void init(CommonAttrVO commonAttr) {
        KiteSceneInst kiteSceneInst = querySceneInstService
            .getSceneInst(KiteWebContext.getContext().getCurrentSceneInstId());
        boolean isGift = false;
        if (!KiteObjectUtils.isEmpty(kiteSceneInst)) {
            KiteOfferInst offerInst = querySceneInstService.getOfferInst(KiteWebContext.getContext().getCustId(), kiteSceneInst.getRootInstId());
            if (!KiteObjectUtils.isEmpty(offerInst)) {
                Offer offer = OfferCache.get(offerInst.getOfferId());
                if (!KiteObjectUtils.isEmpty(offer)) {
                    String serviceOfferId = KiteObjectUtils.isEmpty(kiteSceneInst) ? "" : kiteSceneInst.getServiceOfferId();
                    if (KiteStringUtils.isEqual(CuccServiceOfferConsts.SERVICE_OFFER_RETURN, serviceOfferId)
                        && KiteStringUtils.isEqual(KeyConsts.OFFER_TYPE_15, offer.getOfferType())) {
                        isGift = true;
                    }
                }
            }
        }
        if (!isGift) {
            commonAttr.setIsNull("T");
            commonAttr.setShowType("E");
            KiteAttr attr = querySceneInstService
                .getCommonAttr(KiteWebContext.getContext().getCurrentSceneInstId(), commonAttr.getFieldName());
            attr.setIsNull("T");
            attr.setShowType("E");
            operateSceneInstService.setCommonAttr(KiteWebContext.getContext().getCurrentSceneInstId(), attr);
        }
    }
}
